﻿namespace Mockingbird.Repository.MongoDB.Repository.Newsfeed
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Cryptography;
    using Mockingbird.Core.Models;
    using Mockingbird.Core.Repository;
    using global::MongoDB.Bson;
    using global::MongoDB.Driver;
    using global::MongoDB.Driver.Builders;

    public class NewsFeedRepository : INewsFeedRepository
    {
        private MongoCollection<NewsFeedModel> newsCollection;

        public NewsFeedRepository(MongoCollection<NewsFeedModel> newsCollection)
        {
            this.newsCollection = newsCollection;
        }

        public List<NewsFeedModel> GetLatestNews(List<ObjectId> friends)
        {
            if (friends == null)
            {
                return new List<NewsFeedModel>();
            }

            var query =
               Query.In("AuthorId", friends.Select(id => new BsonDocument { 
                {"$ref", "users"}, 
                {"$id", id} 
            }));

            var newsFeeds = newsCollection.Find(query).SetSortOrder(SortBy.Descending("Date")).ToList();

            foreach (var newsFeedModel in newsFeeds)
            {
                newsFeedModel.Author = newsCollection.Database.FetchDBRefAs<UserPropertiesModel>(newsFeedModel.AuthorId);
            }

            return newsFeeds;
        }

        public List<NewsFeedModel> GetMyNewsFeed(ObjectId myId)
        {
            var refDocument = new BsonDocument { 
                {"$ref", "users"}, 
                {"$id", myId} 
            };

            var query = Query.EQ("AuthorId", refDocument);
            var cursor = newsCollection.Find(query);
            cursor.Limit = 50;
            cursor.SetSortOrder(SortBy.Descending("Date"));

            var newsFeeds = cursor.ToList();

            foreach (var newsFeedModel in newsFeeds)
            {
                newsFeedModel.Author = newsCollection.Database.FetchDBRefAs<UserPropertiesModel>(newsFeedModel.AuthorId);
            }


            return newsFeeds;
        }

        public ObjectId CreateEvent(NewsFeedModel news)
        {
            newsCollection.Insert(news);

            return news.Id;
        }

        public List<NewsFeedModel> GetHashTags(string searchText)
        {
            IMongoQuery qq = new QueryDocument();

            foreach (string str in searchText.Split('#').Select(s => '#' + s))//.Where(s => s.StartsWith("#")))
            {
                if (str.Equals("#"))
                    continue;
                var regex = new BsonRegularExpression(str, "i");
                qq = Query.And(qq, Query.Matches("Text", regex));
            }

            var newsFeeds = newsCollection.Find(qq).SetSortOrder(SortBy.Descending("Date")).ToList();

            foreach (var newsFeedModel in newsFeeds)
            {
                newsFeedModel.Author = newsCollection.Database.FetchDBRefAs<UserPropertiesModel>(newsFeedModel.AuthorId);
            }

            return newsFeeds;
        }
    }
}
